#include "CustomTempDiff.h"
#include "../TinyXML/tinyxml.h"
#include <QDebug>
#include "../ChineseEnglishMapping.h"
#include <QVBoxLayout>
#include <QSplitter>

CustomTempDiff::CustomTempDiff()
{

}

void CustomTempDiff::InitUI(const QString &fileName, QList<QPair<QWidget *, QString> > &widgets, QMap<int, QList<TableWidgetForm *> > &m_pTableWidgetFormMap)
{
    QString filePath = QString("%1/config/%2/%3")
            .arg(COMMONS->XmlPath)
            .arg(COMMONS->MachType)
            .arg(fileName);

    QList<QList<Slotstruct>> _SlotstructLists;
    CustomTempDiff::Analysis(filePath, _SlotstructLists);

    //添加第一个表
    QWidget* widget = new QWidget;
    QVBoxLayout *layout = new QVBoxLayout(widget);
    QSplitter *splitter = new QSplitter(Qt::Vertical, widget);
    splitter->setChildrenCollapsible(false);
    layout->addWidget(splitter);
    widgets.append(qMakePair(widget,QStringLiteral("tab0")));

    for (int i= 0;i<_SlotstructLists.size();i++)
    {
        TableWidgetForm* _TableWidgetForm = new TableWidgetForm(widget);
        splitter->addWidget(_TableWidgetForm);
        _TableWidgetForm->setGroupBoxTitle(QString("%1_0").arg(fileName.left(fileName.size()-4)));
        //获取表头
        Slotstruct _tempSlotstruct;
        _TableWidgetForm->setTableWidgetHeaderLabels(COMMONS->getTableWidgetHeadLabels(fileName,_tempSlotstruct.toFieldNames().split(',')));
        _TableWidgetForm->setTableWidgetHeaderType(_tempSlotstruct.toFieldTypes());

        QList<Slotstruct> _Slotstructs = _SlotstructLists.at(i);
        for(int i = 0;i<_Slotstructs.size();i++)
        {
            int rowCount = _TableWidgetForm->getTableWidget()->rowCount();
            _TableWidgetForm->getTableWidget()->setRowCount(rowCount+1);
            _TableWidgetForm->getTableWidget()->setItem(rowCount, 0, new QTableWidgetItem(QString("%1").arg(_Slotstructs.at(i).out_sensor)));
            _TableWidgetForm->getTableWidget()->item(rowCount,0)->setData(Qt::UserRole,_Slotstructs.at(i).id);
            _TableWidgetForm->getTableWidget()->setItem(rowCount, 1, new QTableWidgetItem(QString("%1").arg(_Slotstructs.at(i).in_sensor)));
        }
        QList<TableWidgetForm*> _TableWidgetFormList = m_pTableWidgetFormMap.value(0);
        m_pTableWidgetFormMap.insert(0,  _TableWidgetFormList << _TableWidgetForm);
    }
}

bool CustomTempDiff::SaveUI(const QString &fileName, const QMap<int, QList<TableWidgetForm *> > &m_pTableWidgetFormMap)
{
    QString filePath = QString("%1/config/%2/%3")
            .arg(COMMONS->XmlPath)
            .arg(COMMONS->MachType)
            .arg(fileName);
    if(m_pTableWidgetFormMap.count() == 1)
    {
        QList<QList<Slotstruct>> _SlotstructLists;
        for(int i = 0;i<m_pTableWidgetFormMap.value(0).size();i++)
        {
            QList<Slotstruct> _Slotstructs;
            TableWidgetForm* _TableWidgetForm = m_pTableWidgetFormMap.value(0).at(i);
            if(_TableWidgetForm)
            {
                for (int k = 0;k<_TableWidgetForm->getTableWidget()->rowCount();k++)
                {
                    Slotstruct _Slotstruct;
                    _Slotstruct.id = _TableWidgetForm->getTableWidget()->item(k,0)->data(Qt::UserRole).toInt();
                    _Slotstruct.out_sensor = _TableWidgetForm->getTableWidget()->item(k,0)->text().toInt();
                    _Slotstruct.in_sensor = _TableWidgetForm->getTableWidget()->item(k,1)->text().toInt();
                    _Slotstructs.append(_Slotstruct);
                }
            }
            _SlotstructLists.append(_Slotstructs);
        }
        CustomTempDiff::SaveXml(filePath,_SlotstructLists);
        return true;
    }
    return false;
}

void CustomTempDiff::Analysis(const QString &path, QList<QList<Slotstruct>> &slot)
{
    TiXmlDocument* xmlDocument = new TiXmlDocument();
    if(!xmlDocument->LoadFile(path.toStdString().c_str()))
    {
        qDebug() << xmlDocument->ErrorDesc();
        delete xmlDocument;
        return;
    }

    //查找xml文件中第一个名为Alarms的节点
    TiXmlElement* rootElement = xmlDocument->FirstChildElement("CustomTempDiff");
    if(rootElement == nullptr) //节点不存在
    {
        qDebug() << xmlDocument->ErrorDesc();
        delete xmlDocument;
        return;
    }

    if(rootElement)
    {
        for (TiXmlElement* AreaElement = rootElement->FirstChildElement("Area");
             AreaElement != nullptr; AreaElement = AreaElement->NextSiblingElement("Area"))
        {
            QList<Slotstruct> _slotList;
            for (TiXmlElement* SlotElement = AreaElement->FirstChildElement("Slot");
                 SlotElement != nullptr; SlotElement = SlotElement->NextSiblingElement("Slot"))
            {
                if(SlotElement != nullptr)
                {
                    Slotstruct _slot;
                    SlotElement->QueryIntAttribute("id", &_slot.id);
                    SlotElement->QueryIntAttribute("out_sensor", &_slot.out_sensor);
                    SlotElement->QueryIntAttribute("in_sensor", &_slot.in_sensor);
                    _slotList.append(_slot);
                }
            }
            slot.append(_slotList);
        }
    }
    delete xmlDocument;
}

bool CustomTempDiff::SaveXml(const QString &path, QList<QList<Slotstruct> > &slot)
{
    TiXmlDocument* xmlDocument = new TiXmlDocument();
    if(!xmlDocument->LoadFile(path.toStdString().c_str()))
    {
        qDebug() << xmlDocument->ErrorDesc();
        delete xmlDocument;
        return false;
    }

    //查找xml文件中第一个名为Person的节点
    TiXmlElement* rootElement = xmlDocument->FirstChildElement("CustomTempDiff");
    if(rootElement == nullptr) //节点不存在
    {
        qDebug() << xmlDocument->ErrorDesc();
        delete xmlDocument;
        return false;
    }

    if(rootElement)
    {
        int index = 0;
        for (TiXmlElement* AreaElement = rootElement->FirstChildElement("Area");
             AreaElement != nullptr; AreaElement = AreaElement->NextSiblingElement("Area")) {
            if(AreaElement != nullptr)
            {
                for (TiXmlElement* SlotElement = AreaElement->FirstChildElement("Slot");
                     SlotElement != nullptr; SlotElement = SlotElement->NextSiblingElement("Slot")) {
                    if(SlotElement != nullptr)
                    {
                        AreaElement->RemoveChild(SlotElement);
                    }
                }
                for (auto _SlotStruct : slot.at(index))
                {
                    //元素类
                    TiXmlElement* ChildxmlElement = new TiXmlElement("Slot");
                    ChildxmlElement->SetAttribute("id", _SlotStruct.id);
                    ChildxmlElement->SetAttribute("out_sensor", _SlotStruct.out_sensor);
                    ChildxmlElement->SetAttribute("in_sensor", _SlotStruct.in_sensor);
                    AreaElement->LinkEndChild(ChildxmlElement);
                }
                index++;
            }
        }
    }

    //保存
    xmlDocument->SaveFile(path.toStdString().c_str());

    delete xmlDocument;
    return true;
}
